package com.xk857.forum.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xk857.entities.Forum;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xk857.feign.req.UserInfoREQ;
import com.xk857.forum.mapper.provider.ForumProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 星空小屋-论坛话题表 Mapper 接口
 *
 * @author CV大魔王
 * @since 2021-02-23
 */
public interface ForumMapper extends BaseMapper<Forum> {

    /**
     * 根据标签id分页查询话题列表
     *
     * @param page    分页参数
     * @param labelId 标签id
     * @return
     */
    @Select("SELECT DISTINCT xf.* FROM xk857_forum xf LEFT JOIN xk857_forum_label xfl  ON xf.id = xfl.forum_id WHERE xf.`status` != 0 AND xfl.label_id = #{labelId} ORDER BY xf.update_date DESC")
    IPage<Forum> findListByLabelId(IPage<Forum> page, @Param("labelId") String labelId);


    /**
     * 根据话题ID查询话题详情与标签ids
     *
     * @param id 话题id
     * @return Forum对象
     */
    @Select("SELECT id,user_id,nick_name,user_image,title,md_content,html_content,view_count,thumhup,reply,`status`,create_date,update_date FROM xk857_forum where id = #{id}")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "id", property = "labelIds", javaType = List.class, many = @Many(select = "com.xk857.forum.mapper.ForumMapper.findLabelIdByForumId"))
    })
    Forum findForumAndLabelIdsById(String id);


    /**
     * 根据ForumId查询标签id
     *
     * @return 标签id列表
     */
    @Select("SELECT label_id FROM xk857_forum_label WHERE forum_id = #{id}")
    List<String> findLabelIdByForumId(String id);


    /**
     * 通过话题 id 删除话题标签中间表
     *
     * @param forumId 话题id
     * @return 逻辑值
     */
    @Delete("DELETE FROM xk857_forum_label where forum_id = #{forumId}")
    boolean deleteForumLabel(@Param("forumId") String forumId);


    /**
     * 新增话题标签中间表数据
     *
     * @param forumId 话题id
     * @param labelIds 标签集合
     * @return 逻辑值
     */
    @InsertProvider(type = ForumProvider.class, method = "saveForumLabel")
    boolean saveForumLabel(@Param("forumId") String forumId, @Param("labelIds") List<String> labelIds);

    /**
     * 更新问题与回答表中的用户信息
     * @param req 用户信息
     * @return 逻辑值
     */
    @Update("UPDATE xk857_forum SET nick_name = #{nickName}, user_image = #{userImage} WHERE user_id = #{userId}; UPDATE xk857_replay SET nick_name = #{nickName}, user_image = #{userImage} WHERE user_id = #{userId}")
    boolean updateUserInfo(UserInfoREQ req);


    /**
     * 查询今日帖子数量
     * @return
     */
    @Select("select count(id) from xk857_forum where to_days(update_date) = to_days(now())")
    Integer searchCountDayNum();


    /**
     * 查询昨日帖子数量
     * @return
     */
    @Select("SELECT count(id) FROM xk857_forum WHERE TO_DAYS(NOW()) - TO_DAYS(update_date) <= 1")
    Integer searchNumberOfArticlesInYesterdaySDay();


    /**
     * 查询近七日帖子数量
     * @return
     */
    @Select("SELECT count(id) FROM xk857_forum where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(update_date)")
    Integer searchNumberOfArticlesInWeek();
}
